package www.sharetime.weshare.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import www.sharetime.weshare.R;
import www.sharetime.weshare.base.BaseFragment;
import www.sharetime.weshare.event.NetWorkEvent;
import www.sharetime.weshare.view.NavigationButton;

/**
 * File Description  : 底部导航fragment
 *
 * @author : zhanggeng
 * @version : v1.0
 * @email : zhanggengdyx@gmail.com
 * @date : 2017/9/12 19:55
 * **************修订历史*************
 */
public class NavFragment extends BaseFragment implements View.OnClickListener{

    View mBaseView;

//    @BindView(R.id.nav_item_home)
    NavigationButton mNavHome;
//    @BindView(R.id.nav_item_message)
    NavigationButton mNavMessage;
//    @BindView(R.id.nav_item_me)
    NavigationButton mNavMe;


    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationButton mCurrentNavButton;
    private OnNavigationReselectListener mOnNavigationReselectListener;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nav;
    }

    @Override
    public View initView() {

        mBaseView = inflateView(getLayoutId());

//        unbind = ButterKnife.bind(getActivity(), mBaseView);

        mNavHome = (NavigationButton) mBaseView.findViewById(R.id.nav_item_home);
        mNavHome.setOnClickListener(this);
        mNavMessage = (NavigationButton) mBaseView.findViewById(R.id.nav_item_message);
        mNavMessage.setOnClickListener(this);
        mNavMe = (NavigationButton) mBaseView.findViewById(R.id.nav_item_me);
        mNavMe.setOnClickListener(this);

        mNavHome.init(R.drawable.tab_icon_home,
                R.string.nav_icon_home,
                HomeFragment.class);

        mNavMessage.init(R.drawable.tab_icon_message,
                R.string.nav_icon_msg,
                MessageFragment.class);

        mNavMe.init(R.drawable.tab_icon_me,
                R.string.nav_icon_me,
                PersonalFragment.class);

        return mBaseView;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.nav_item_home:
            case R.id.nav_item_message:
            case R.id.nav_item_me:
                if (v instanceof NavigationButton) {
                    NavigationButton nav = (NavigationButton) v;
                    doSelect(nav);
                } else {
                    //TODO： 为中间发布动态的按钮预留
                }
                break;
        }
    }




//    @OnClick({R.id.nav_item_home, R.id.nav_item_message, R.id.nav_item_me})
//    public void onClick(View view) {
//
//
//        if (view instanceof NavigationButton) {
//            NavigationButton nav = (NavigationButton) view;
//            doSelect(nav);
//        } else {
//            //TODO： 为中间发布动态的按钮预留
//        }
//
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetTotalUnreadCount(NetWorkEvent updateMsgReadStateEvent) {
       //TODO:
    }


    public void setup(Context context, FragmentManager fragmentManager, int contentId, OnNavigationReselectListener listener) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        mOnNavigationReselectListener = listener;

        // do clear
        clearOldFragment();
        // do select first
        doSelect(mNavHome);
    }

    public void select(int index) {
        if (mNavMe != null)
            doSelect(mNavMe);
    }

    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commitNow();
    }

    private void doSelect(NavigationButton newNavButton) {
        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;

    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), null);
                ft.add(mContainerId, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }

    private void onReselect(NavigationButton navigationButton) {
        OnNavigationReselectListener listener = mOnNavigationReselectListener;
        if (listener != null) {
            listener.onReselect(navigationButton);
        }
    }


    public interface OnNavigationReselectListener {
        void onReselect(NavigationButton navigationButton);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initData() {
        super.initData();
    }
}
